package com.qf.springboot.invalidator.controller;

import com.qf.springboot.invalidator.groups.Login;
import com.qf.springboot.invalidator.groups.Register;
import com.qf.springboot.invalidator.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;



@RestController
@Api(value = "用户的控制器",tags = {"用户的控制器"})
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "/Register",method = RequestMethod.POST)
    @ApiOperation(value = "测试接口1",notes="调用当前方法的注意事项")
    public String Register(@Validated(Register.class) User user, BindingResult result){
        if (result.hasFieldErrors()){

            List<FieldError> fieldErrors = result.getFieldErrors();

            for (FieldError fieldError : fieldErrors) {
                fieldError.getField();
                fieldError.getDefaultMessage();
                logger.info("校验出错的列是:"+ fieldError.getField()+"----校验的提示信息是:"+fieldError.getDefaultMessage());
            }
        }
        return "校验完成";
    }
    @RequestMapping(value = "/Login",method = RequestMethod.POST)
    @ApiOperation(value = "测试接口2",notes="调用当前方法的注意事项")
    public String Login(@Validated(Login.class) User user, BindingResult result){
        if (result.hasFieldErrors()){

            List<FieldError> fieldErrors = result.getFieldErrors();

            for (FieldError fieldError : fieldErrors) {
                fieldError.getField();
                fieldError.getDefaultMessage();
                logger.info("校验出错的列是:"+ fieldError.getField()+"----校验的提示信息是:"+fieldError.getDefaultMessage());
            }
        }
        return "校验完成1";
    }
}
